package org.jetnuke.util;

import org.apache.log4j.Logger;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Locale;

public class Formatter {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(Formatter.class);

	public String format(Double db, Locale locale) {
		if (logger.isDebugEnabled()) {
			logger.debug("format(Double, Locale) - start"); //$NON-NLS-1$
		}

		try {
			String returnString = format(db.doubleValue(), locale);
			if (logger.isDebugEnabled()) {
				logger.debug("format(Double, Locale) - end"); //$NON-NLS-1$
			}
			return returnString;
		} catch (Throwable e) {
			logger.warn("format(Double, Locale) - exception ignored", e); //$NON-NLS-1$
		}

		if (logger.isDebugEnabled()) {
			logger.debug("format(Double, Locale) - end"); //$NON-NLS-1$
		}
		return "";
	}

	public String format(double db, Locale locale) {
		if (logger.isDebugEnabled()) {
			logger.debug("format(double, Locale) - start"); //$NON-NLS-1$
		}

		DecimalFormat fmt = (DecimalFormat) NumberFormat.getInstance(locale);
		try {
			fmt.applyPattern("########0.0#");
			String returnString = fmt.format(db);
			if (logger.isDebugEnabled()) {
				logger.debug("format(double, Locale) - end"); //$NON-NLS-1$
			}
			return returnString;
		} catch (Throwable e) {
			logger.warn("format(double, Locale) - exception ignored", e); //$NON-NLS-1$
		}

		if (logger.isDebugEnabled()) {
			logger.debug("format(double, Locale) - end"); //$NON-NLS-1$
		}
		return "";
	}

	public String percent(double value, double total, Locale locale) {
		if (logger.isDebugEnabled()) {
			logger.debug("percent(double, double, Locale) - start"); //$NON-NLS-1$
		}

		DecimalFormat fmt = (DecimalFormat) NumberFormat.getInstance(locale);
		try {
			fmt.applyPattern("########0.0#");
			double percent = 0.;
			if (value != 0 || total != 0) {
				percent = value * 100. / total;
			}
			String returnString = fmt.format(percent);
			if (logger.isDebugEnabled()) {
				logger.debug("percent(double, double, Locale) - end"); //$NON-NLS-1$
			}
			return returnString;
		} catch (Throwable e) {
			logger.warn(
					"percent(double, double, Locale) - exception ignored", e); //$NON-NLS-1$
		}

		if (logger.isDebugEnabled()) {
			logger.debug("percent(double, double, Locale) - end"); //$NON-NLS-1$
		}
		return "";
	}

	public String percent(int value, int total, Locale locale) {
		if (logger.isDebugEnabled()) {
			logger.debug("percent(int, int, Locale) - start"); //$NON-NLS-1$
		}

		DecimalFormat fmt = (DecimalFormat) NumberFormat.getInstance(locale);
		try {
			fmt.applyPattern("########0.0#");
			double percent = 0.;
			if (value != 0 || total != 0) {
				percent = value * 100. / total;
			}
			String returnString = fmt.format(percent);
			if (logger.isDebugEnabled()) {
				logger.debug("percent(int, int, Locale) - end"); //$NON-NLS-1$
			}
			return returnString;
		} catch (Throwable e) {
			logger.warn("percent(int, int, Locale) - exception ignored", e); //$NON-NLS-1$
		}

		if (logger.isDebugEnabled()) {
			logger.debug("percent(int, int, Locale) - end"); //$NON-NLS-1$
		}
		return "";
	}

	public String percent(String value_, String total_, Locale locale) {
		if (logger.isDebugEnabled()) {
			logger.debug("percent(String, String, Locale) - start"); //$NON-NLS-1$
		}

		DecimalFormat fmt = (DecimalFormat) NumberFormat.getInstance(locale);
		try {
			fmt.applyPattern("########0.0#");
			double percent = 0.;
			double value = Double.parseDouble(value_);
			double total = Double.parseDouble(total_);
			if (value != 0 || total != 0) {
				percent = value * 100. / total;
			}
			String returnString = fmt.format(percent);
			if (logger.isDebugEnabled()) {
				logger.debug("percent(String, String, Locale) - end"); //$NON-NLS-1$
			}
			return returnString;
		} catch (Throwable e) {
			logger.warn(
					"percent(String, String, Locale) - exception ignored", e); //$NON-NLS-1$
		}

		if (logger.isDebugEnabled()) {
			logger.debug("percent(String, String, Locale) - end"); //$NON-NLS-1$
		}
		return "";
	}

	public String format(String db, Locale locale) {
		if (logger.isDebugEnabled()) {
			logger.debug("format(String, Locale) - start"); //$NON-NLS-1$
		}

		try {
			String returnString = format(Double.parseDouble(db), locale);
			if (logger.isDebugEnabled()) {
				logger.debug("format(String, Locale) - end"); //$NON-NLS-1$
			}
			return returnString;
		} catch (Throwable e) {
			logger.warn("format(String, Locale) - exception ignored", e); //$NON-NLS-1$

		}

		if (logger.isDebugEnabled()) {
			logger.debug("format(String, Locale) - end"); //$NON-NLS-1$
		}
		return "";
	}

	public String shorten(String txt, int count) {
		if (logger.isDebugEnabled()) {
			logger.debug("shorten(String, int) - start"); //$NON-NLS-1$
		}

		if (txt == null)
			return "";
		int length = txt.length();
		if (length <= count)
			return txt;
		String returnString = txt.substring(0, count) + "...";
		if (logger.isDebugEnabled()) {
			logger.debug("shorten(String, int) - end"); //$NON-NLS-1$
		}
		return returnString;
	}

}
